/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.lnu.ums.dal.impl;

import edu.lnu.ums.dal.IFacultyDAO;
import edu.lnu.ums.entities.Faculty;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;


@Stateless
public class FacultyDAO extends GenericUMSObjectDAO<Faculty> implements IFacultyDAO{

    public FacultyDAO() {
        super(Faculty.class);
    }

    @Override
    public Faculty getByName(EntityManagerFactory factory, String name) {
        EntityManager em = factory.createEntityManager();
        List<Faculty> faculties = em.createQuery("from Faculty where fullName = :name")
                .setParameter("name", name).getResultList();

        em.close();
        setEntityManagerFactoryToEntityList(factory, faculties);
        if (faculties.size() == 0) {
            return null;
        }

        if (faculties.size() > 1) {
            throw new RuntimeException("More than one faculty with same name");
        }

        return faculties.get(0);
    }
}
